from animation import animation, animation_single, tiler
import image
from camera import camera
import random

class level (object) :
    """ Container for each level of the building """
    def __init__ (self, tower, level) :
        """ Initialize: load images, etc """
        self.cam = camera ()
        world_x, world_y = self.cam.get_world ()
        self.x = world_x
        self.x2 = 0
        self.level = level
        self.y = (world_y - 360) - (level * 36)
        self.tower = tower
        self.buildings = []
        self.fire_escape_l = animation_single (image.load ("fire_escape_l.png"))
        self.fire_escape_r = animation_single (image.load ("fire_escape_r.png"))
    def add_building (self, building) :
        """ Add a building to the level """
        self.buildings.append (building)
        if building.x < self.x :
            self.x = building.x
        if building.x2 > self.x2 :
            self.x2 = building.x2
        self.fire_escape_l.set_position (self.x - 24, self.y)
        self.fire_escape_r.set_position (self.x2, self.y)
    def update (self, dt) :
        """ Update all of the buildings in this level """
        for building in self.buildings :
            building.update (dt)
    def draw (self) :
        """ Draw all of the buildings in this level (and the fire escape) """
        for building in self.buildings :
            building.draw ()
        if not self.level == 1 :
            self.fire_escape_l.draw ()
            self.fire_escape_r.draw ()
    

class building (object) :
    """ Building object to be extended by each type of building """
    def __init__ (self, tower, x, level, x2) :
        """ Init: add building to correct level in tower, setup pos, etc """
        self.tower = tower
        level += 1
        self.x = x
        self.x2 = x2
        self.cam = camera ()
        world_x, world_y = self.cam.get_world ()
        self.y = (world_y - 360) - (level * 36)
        self.dt = 0
        self.states = {}
        self.animations = {}
        self.current_state = None
        self.current_animation = None
        tower.add_building (level, self)
        tower.map.set_callback (level, self.x, self.x2, self)
    def update (self, dt) :
        """ Update the building's current state and the building's
            current animation """
        self.current_state (dt)
        self.current_animation.update (dt)
    def draw (self) :
        """ Draw the current animation """
        self.current_animation.draw ()

class office (building) :
    """ Office object """
    def __init__ (self, tower, x, level) :
        """ Init: load animations, setup states """
        building.__init__ (self, tower, x, level, x + 72)
        self.states['unoccupied_day'] = office.unoccupied_day
        self.states['occupied_day'] = office.occupied_day
        self.animations['unoccupied_day'] = animation ()
        self.animations['unoccupied_day'].load ('office.ini', 'unoccupied_day')
        self.animations['occupied_day'] = animation ()
        self.animations['occupied_day'].load ('office.ini', 'occupied_day')
        self.animations['unoccupied_day'].set_position (self.x, self.y)
        self.animations['occupied_day'].set_position (self.x, self.y)
        self.current_state = self.occupied_day
        self.current_animation = self.animations['occupied_day']
    def unoccupied_day (self, dt) :
        """ State for when this office is unoccupied during the day """
        self.dt += 1
        if self.dt > random.randint (30, 200) :
            self.current_state = self.occupied_day
            self.current_animation = self.animations['occupied_day']
            self.dt = 0
    def occupied_day (self, dt) :
        """ Occupied during the day state """
        self.dt += 1
        if self.dt > random.randint (30, 2000) :
            self.current_state = self.unoccupied_day
            self.current_animation = self.animations['unoccupied_day']
            self.dt = 0

class lobby_ground (building) :
    """ Ground level lobby """
    def __init__ (self, tower, x, x2) :
        """ Init: load images and etc """
        building.__init__ (self, tower, x, 0, x2)
        self.lobby_tiles = tiler (image.load ('lobby.png'), self.x, self.x2, self.y)
        self.awn_right = animation_single (image.load ('awn_right.png'))
        self.awn_left = animation_single (image.load ('awn_left.png'))
        self.do_calcs ()
    def do_calcs (self) :
        """ Calculate the awning positions, tiler stuff """
        self.awn_right.set_position (self.x2, self.y)
        self.awn_left.set_position (self.x - 56, self.y)
    def update (self, dt) :
        """ no updating required ;P """
        pass
    def set_position (self, x, x2) :
        """ Make the lobby longer """
        self.x = x
        self.x2 = x2
        self.do_calcs ()
    def draw (self) :
        """ Draw the lobby and the awnings on the left and right """
        self.lobby_tiles.draw ()
        self.awn_right.draw ()
        self.awn_left.draw ()
        